package com.fand.acceptControl.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.joda.time.DateTime;

import com.fand.acceptControl.dto.NavigationDto;
import com.fand.persistent.builder.ModelArgs;
import com.fand.persistent.builder.ModelBuilder;
import com.fand.persistent.builder.ModelBuilderUtils;
import com.fand.persistent.support.JdbcDaoSupport;
import com.google.common.collect.Maps;

/**
 * 导航
 * 
 * @author haitao.yao
 * 
 */
public class JdbcNavigationDao extends JdbcDaoSupport implements NavigationDao {

	@Override
	public String saveNavigationBy1ForObject(final NavigationDto navigation) {
		if (StringUtils.isBlank(navigation.getUrl())) {
			navigation.setUrl("#" + navigation.getCode());
		}

		final ModelArgs modelArgs = new ModelArgs(null, navigation, "code", "siteId", "typeId");

		final ModelBuilder selectModelBuilder = ModelBuilderUtils.newModelBuilder(modelArgs);

		final NavigationDto oldNavigation = this.selectForMappedObject(NavigationDto.class,
				"acceptControl/navigation/selectNavigationBy1ForMappedObjectList", selectModelBuilder.build());

		if (oldNavigation == null) {
			final Date deginDaTi = new Date();
			final DateTime endDaTi = new DateTime(9999, 12, 31, 0, 0, 0, 0);

			navigation.setBeginDaTi(deginDaTi);
			navigation.setEndDaTi(endDaTi.toDate());
			navigation.setId(UUID.randomUUID().toString());

			final ModelBuilder insertModelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.INSERT, navigation));

			this.update("acceptControl/navigation/insertNavigationBy1", insertModelBuilder.build());

			return navigation.getId();
		}

		do {
			if (!StringUtils.equals(oldNavigation.getSiteId(), navigation.getSiteId())) {
				break;
			}

			if (!StringUtils.equals(oldNavigation.getStateId(), navigation.getStateId())) {
				break;
			}

			if (!StringUtils.equals(oldNavigation.getParentId(), navigation.getParentId())) {
				break;
			}

			if (!StringUtils.equals(oldNavigation.getTypeId(), navigation.getTypeId())) {
				break;
			}

			if (!StringUtils.equals(oldNavigation.getName(), navigation.getName())) {
				break;
			}

			if (NumberUtils.compare(oldNavigation.getOrderNum(), navigation.getOrderNum()) != 0) {
				break;
			}

			if (!StringUtils.equals(oldNavigation.getUrl(), navigation.getUrl())) {
				break;
			}

			if (!StringUtils.equals(oldNavigation.getTarget(), navigation.getTarget())) {
				break;
			}

			if (!StringUtils.equals(oldNavigation.getDataOptions(), navigation.getDataOptions())) {
				break;
			}

			return oldNavigation.getId();
		} while (false);

		final ModelBuilder updateModelBuilder = ModelBuilderUtils.newModelBuilder(
				new ModelArgs(ModelBuilder.UPDATE, navigation, "siteId", "stateId", "parentId", "typeId", "name", "orderNum", "url",
						"target", "dataOptions")).putExtend(null, "id", null, oldNavigation.getId());

		this.update("acceptControl/navigation/updateNavigationBy1", updateModelBuilder.build());

		return oldNavigation.getId();
	}

	@Override
	public List<NavigationDto> selectNavigationBy2ForMappedObjectList(final NavigationDto selectNavigationBy) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put("SITE_ID", selectNavigationBy.getSiteId());
		model.put("TYPE_CODE", selectNavigationBy.getTypeCo());

		return this.selectForMappedObjectList(NavigationDto.class, "acceptControl/navigation/selectNavigationBy2ForMappedObjectList",
				model);
	}

	@Override
	public List<NavigationDto> selectNavigationBy3ForMappedObjectList(final NavigationDto selectNavigationBy) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put("SITE_ID", selectNavigationBy.getSiteId());
		model.put("TYPE_ID", selectNavigationBy.getTypeId());
		model.put("PARENT_ID", selectNavigationBy.getParentId());

		return this.selectForMappedObjectList(NavigationDto.class, "acceptControl/navigation/selectNavigationBy2ForMappedObjectList",
				model);
	}
}